/*
 * Copyright (C) <2009>  <Matthew Dickinson>
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.pulsarwebdevelopment.concordance;

import java.util.*;
import java.io.*;
import java.sql.*;

/**
 * Provides basic tools (open, save, etc.) for concordances
 * @author Matthew Dickinson
 * Last updated: 7/20/2009
 */
public class ConcordanceTool {
	/**
	 *  
	 */
	private Concordance concordance;

	private PreparedStatement getConcs;

	private DatabaseConnection dbCon;

	private File baseConcDir;
	/**
	 * constructor
	 */
	public ConcordanceTool(File baseDir){
		this.baseConcDir = baseDir;

		try{
			dbCon = new DatabaseConnection();
			dbCon.connect(baseDir);
			getConcs = dbCon.dbConnection.prepareStatement("select id, title from concordance_table");
		}catch(Exception e){
			e.printStackTrace();
		}
		//DO THINGS...MAYBE??
		//compileConcordance();
	}

	public void compileConcordance(File file, String title, String description){
		//int conc = Integer.parseInt(JOptionPane.showInputDialog("Enter the id of the concordance you wish to process"));
		TextProcessor proc = new TextProcessor(title, description, this.baseConcDir);
		this.concordance = proc.process(file);
	}

	public void compileConcordance(File file, int concordance){
		TextProcessor proc = new TextProcessor(concordance, this.baseConcDir);
		this.concordance = proc.process(file);				//FIX
	}

	public Concordance getConcordance(){
		return concordance;
	}

	public ArrayList<Concordance.ConcordanceInfo> getConcordances(){
		ArrayList<Concordance.ConcordanceInfo> concs = new ArrayList<Concordance.ConcordanceInfo>();
		//concs.add(new ConcordanceInfo(0, "Test"));
		try {
			ResultSet rs = getConcs.executeQuery();
			while(rs.next()){
				concs.add(new Concordance.ConcordanceInfo(rs.getInt("id"), rs.getString("title")));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		//concs.add(new Concordance.ConcordanceInfo(0, "Test"));
		
		return concs;
	}

	/**
	 * returns the set of words that only occurs the specified number of times
	 * of "set" of 0 returns the full concordance
	 * @param set number of occurences 
	 * @return a TreeMap of words that only occur "set" times
	 */
	public String getWordSet(int set){
		StringWriter outStr = new StringWriter();
		ConcordanceOutput<String, Verse> output = 
			new ConcordanceOutputStream<String, Verse>(outStr, ConcordanceOutputStream.ListStyle.ONE_PER_LINE_TRUNCATED);
		concordance.getWordSetWithVerses(set, output);
		return outStr.toString();
	}


	/**
	 * 
	 * @param concordance the id of the concordance to be opened
	 */
	public void openConcordance(int conc){
		this.concordance = new Concordance(conc, this.baseConcDir);
	}

}
